const router = require('koa-router')()
const userserver = require('../controllers/mySqlConfig')
router.prefix('/diary')//公共前缀
//个人呢根据前端传给的类型，查找对应的文章列表

router.post('/findNoteListByType',async (ctx,next)=>{
    console.log(ctx.request.body);
    let {note_type,userid} = ctx.request.body
    console.log(note_type);
   await userserver.findfindNoteListByType(note_type,userid).then((res)=>{
    // console.log(res);
        ctx.body = {
            code:200,
            data:res,
            mess:'查找成功'
        }
   }).catch(err =>{
    ctx.body = {
        code:'5265',
        data:err,
        mess:'查找失败'
    }
   })
})
//根据前端传递的id，查找对应文章的详情
router.get('/findNoteDetailById', async (ctx,next) =>{
    console.log(ctx.query.id);
    let id = ctx.query.id
   await userserver.findNoteDetailById(id).then(res =>{
    ctx.body = {
        code:200,
        data:res,
        mess:'查询成功'
    }
   }).catch(err =>{
    ctx.body = {
        code:'522222',
        data:err,
        mess:'查找失败'
    }
   })
})

//发布日记
router.post('/publishnote', async (ctx,next)=>{
    console.log(ctx.request.body);
    let {note_content,title,head_img,note_type,nickname,userid} = ctx.request.body
    let date = new Date()
    let newDay = date.getDate()
    if(newDay < 10){
        newDay = `0${newDay}`
    }
    let newdate = `${date.getFullYear()}年${date.getMonth()+1}月${newDay}`
    //添加进数据库
    await userserver.insertdiary([note_content,title,head_img,note_type,nickname,userid,newdate,newdate]).then(res=>{
        console.log(res);
        ctx.body = {
            code:200,
            data:res,
            mess:'发布成功'
        }
    }).catch(err=>{
        ctx.body = {
            code:'156223',
            data:err,
            mess:'发布失败'
        }
    })
})
//修改内容
router.post('/revisebtn',async (ctx,next)=>{
    console.log(ctx.request.body);
    let {note_content,title,head_img,note_type,nickname,userid} = ctx.request.body
    let date = new Date()
    let newDay = date.getDate()
    if(newDay < 10){
        newDay = `0${newDay}`
    }
    let newdate = `${date.getFullYear()}年${date.getMonth()+1}月${newDay}`
    await userserver.revisevtn(title,note_content,newdate).then(res=>{
        console.log(res);
        ctx.body = {
            code:200,
            data:res,
            mess:'修改成功'
        }
    }).catch(err =>{
        ctx.body = {
            code:'156223',
            data:err,
            mess:'修改失败'
        }
    })
})
//删除内容
router.post('/deletebtn',async (ctx,next)=>{
    console.log(ctx.request.body);
    let {note_content,userid} = ctx.request.body
    await userserver.deletebtn(note_content,userid).then(res=>{
        console.log(res);
        ctx.body = {
            code:200,
            data:res,
            mess:'删除成功'
        }
    }).catch(err =>{
        ctx.body = {
            code:'156223',
            data:err,
            mess:'删除失败'
        }
    })
})

module.exports = router